from pygame.locals import *
import pygame, sys, random

size = 20
up = 1
down = 2
left = 3
right = 4
mapw = 1400
maph = 800

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)

def main():
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode((mapw, maph))
    pygame.mixer.music.load("bgm.mp3")
    pygame.mixer.music.play()
    screen.fill(blue)
    pygame.display.set_caption("贪吃蛇")
    clock = pygame.time.Clock()
    showstart(screen)
    while True:
        running(screen,clock)
        showend(screen)
#显示开始菜单
def showstart(screen):
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                endgame()
            elif event.type == KEYDOWN:
                if (event.type == K_ESCAPE):
                    endgame()
                else:
                    return
    screen.fill(blue)
#主体
def running(screen,clock):
    startx = random.randrange(size*3, mapw, size)
    starty = random.randrange(0, maph, size)
    body = [{'x': startx, 'y': starty},
            {'x': startx-size, 'y': starty},
            {'x': startx-2*size, 'y': starty}]
    food = {'x': random.randrange(0, mapw, size),
        'y': random.randrange(0, maph, size)}
    direction=right
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                endgame()
            elif event.type == KEYDOWN:
                if event.key == K_LEFT and direction != right:
                    direction = left
                elif event.key == K_RIGHT and direction != left:
                    direction = right
                elif event.key == K_UP and direction != down:
                    direction = up
                elif event.key == K_DOWN and direction != up:
                    direction = down
                elif event.key == K_ESCAPE:
                    endgame()
        pygame.time.delay(50)
        if direction == up:
            new = {'x': body[0]['x'], 'y': body[0]['y']-size}
        elif direction == down:
            new = {'x': body[0]['x'], 'y': body[0]['y']+size}
        elif direction == right:
            new = {'x': body[0]['x']+size, 'y': body[0]['y']}
        elif direction == left:
            new = {'x': body[0]['x']-size, 'y': body[0]['y']}
        body.insert(0, new)
        #死亡判定
        flage = True
        if body[0]['x'] == -size or body[0]['x'] > mapw or body[0]['y'] == -size or body[0]['y'] > maph:
            flage = False
        for bod in body[1:]:
            if bod == body[0]:
                flage = False
        if not flage:
            return
        if body[0] == food:
            food['x'] = random.randrange(0, mapw, size)
            food['y'] = random.randrange(0, maph, size)
            soundwav=pygame.mixer.Sound("texiao.wav") #音效
            soundwav.play()
        else:
            del body[-1]
        #画图
        screen.fill(black)
        for by in body:
            x = by['x']
            y = by['y']
            bo = pygame.Rect(x, y, size, size)
            pygame.draw.rect(screen, (255, 0, 0), bo)
        fd = pygame.Rect(food['x'], food['y'], size, size) #生成食物
        pygame.draw.rect(screen, (255, 255, 255), fd)

        pygame.display.update()
        clock.tick(60)
#显示结束菜单
def showend(screen):
    screen.fill(white)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                endgame()
            elif event.type == KEYDOWN:
                if (event.type == K_ESCAPE):
                    endgame()
                else:
                    return
#退出
def endgame():
    pygame.quit()
    exit()
#运行
main()
